package com.shijie.controller.erp;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import com.github.pagehelper.PageHelper;
import com.shijie.component.ErpUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.NoLogin;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.shijie.domain.entity.Admins;
import com.shijie.domain.entity.Coupons;
import com.shijie.domain.entity.UserCoupons;
import com.shijie.mapper.UserCouponsMapper;
import com.shijie.service.CouponsService;

@RestController
@RequestMapping("/erp/coupons")
public class CouponsController {

	@Resource
	private CouponsService couponseService;
	@Resource
	private UserCouponsMapper userCouponsMapper;
	@Resource
	private ErpUserService erpUserService;
	
    @NoLogin
	@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, "startDate", new CustomDateEditor(dateFormat, true));
        binder.registerCustomEditor(Date.class, "endDate", new CustomDateEditor(dateFormat, true));
    }
    
	@GetMapping
	@RequiresPermissions(value={"coupons:list"})
	public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(required=false) String name,@RequestParam(required=false) Integer status) {
		PageHelper.startPage(page, pageSize);
		Condition condition = new Condition(Coupons.class);
		Example.Criteria c = condition.createCriteria();
		if(!StringUtils.isEmpty(name)){
			c.andLike("name", "%"+name+"%");
		}
		if(status != null) {
			c.andEqualTo("status", status);
		}
		Admins  admin = erpUserService.getAdmin();
		if(admin.getBrandId() != null){
			c.andEqualTo("brandId", admin.getBrandId());
		}
		return ResultGenerator.success(couponseService.selectPage(page, pageSize, condition));
	}
	
	@RequiresPermissions(value={"coupons:create"})
	@Log(title = "优惠券管理", action = BusinessType.INSERT)
	@PostMapping
	public Result add(@RequestBody Coupons couponse) {
		couponseService.save(couponse);
		return ResultGenerator.success();
	}
	
	@RequiresPermissions(value={"coupons:edit"})
	@Log(title = "优惠券管理", action = BusinessType.UPDATE)
	@PutMapping
	public Result edit(@RequestBody Coupons couponse) {
		couponseService.updateById(couponse);
		return ResultGenerator.success();
	}
	
	@RequiresPermissions(value={"coupons:status"})
	@Log(title = "优惠券管理", action = BusinessType.UPDATE)
	@PutMapping("/{id}/{status}")
	public Result edit(@PathVariable("id") Integer id,@PathVariable("status") Integer status) {
		Coupons couponse  = new Coupons();
		couponse.setId(id);
		couponse.setStatus(status);
		couponseService.updateById(couponse);
		return ResultGenerator.success();
	}
	@RequiresPermissions(value={"coupons:delete"})
	@Log(title = "优惠券管理", action = BusinessType.DELETE)
	@DeleteMapping("/{id}")
	public Result delete(@PathVariable("id") Integer id) {
		couponseService.deleteById(id);
		return ResultGenerator.success();
	}
	
	@GetMapping("/{id}")
	public Result get(@PathVariable("id") Integer id) {
		return ResultGenerator.success(couponseService.selectById(id));
	}
	
	@GetMapping("/details/{id}")
	public Result details(@PathVariable("id") Integer id) {
		Condition condition = new Condition(UserCoupons.class);
		condition.createCriteria().andEqualTo("couponId", id);
		return ResultGenerator.success(userCouponsMapper.selectByCondition(condition));
	}
}
